package GUI;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import papers.Manuscript;
import users.Author;
import users.Reviewer;
import conference.Conference;
import conference.ConferenceIO;
/**
 * @author Shawn Paul Smith - ssmith35
 */
public class SubmitPaperPanel extends AbstractEZCPanel {
	private static final long serialVersionUID =
			5496631136267389387L;

	public static final String NAME_PLACEHOLDER =
			"<Name of Paper>";
	
	//UI elements
	private final JFileChooser chooser = new JFileChooser();
	private final JPanel paperpanel = new JPanel(new BorderLayout());
	private final JTextField paperName =
			new JTextField(NAME_PLACEHOLDER, 16);
	private final JLabel viewtitle = new JLabel("");
	private final JButton submitBut =
			new JButton("Submit Manuscript");
	private final ViewSubmittedPapersPanel submittedpanel =
			new ViewSubmittedPapersPanel();
	
	/** Creates a new panel that allows the given author
	 * to submit papers to the given conference. */
	SubmitPaperPanel(Conference conf, Author author,
			String path) {
		super(conf, author, path);		
		setLayout(new BorderLayout());
		//title
		JLabel title = new JLabel("Submit a Manuscript");
		title.setFont(new Font("Sans Serif", Font.BOLD,
				20));
		title.setBorder(BorderFactory
				.createRaisedBevelBorder());
		add(title, BorderLayout.NORTH);
		//Only show UI if there is a program chair
		if (myConf.getPC() != null) {
			//panel to choose paper to submit
			add(new ChoosePanel(), BorderLayout.CENTER);
			//panel to show submitted papers in scrollpane
			paperpanel.add(submittedpanel.getScrollPane(
					new Dimension(1200, 500)),
					BorderLayout.SOUTH);
			//prevent horizontal scrolling
			paperpanel.setMaximumSize(new Dimension(
					getSize().width, Integer.MAX_VALUE));
			add(paperpanel, BorderLayout.SOUTH);
			submittedpanel.refreshPapers();
		}
	}
	
	//Panel for picking and submitting a manuscript
	private class ChoosePanel extends JPanel {
		private static final long serialVersionUID =
				7658054982292761365L;
		public ChoosePanel() {
			//Don't allow submissions if it is too late.
			if (myConf.getManuDate() != null && myConf.getManuDate().before(new Date())) {
				add(new JLabel("No more submissions can" + 
					" be accepted for this conference."));
				return;
			}
			setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
			setAlignmentX(CENTER_ALIGNMENT);
			setSize(new Dimension(1200, 300));
			//Second row
			JPanel filePanel = new JPanel();
			JButton fileChoose = new JButton("Choose File");
			fileChoose.addMouseListener(new ChooseListener());
			filePanel.add(fileChoose);
			filePanel.add(viewtitle);
			add(filePanel);
			//Third row
			JPanel namePanel = new JPanel();
			submitBut.setEnabled(false);
			paperName.setEnabled(false);
			submitBut.addMouseListener(new SubmitListener());
			namePanel.add(new JLabel("Manuscript Name: "));
			namePanel.add(paperName);
			namePanel.add(submitBut);
			add(namePanel);
			//final row
			if (myConf.getManuDate() != null){
				add(new JLabel(String.format("Submission" +
						" accepted until %s", (
							myConf.getManuDate().toString()))));
			}
		}
	}
	
	//Panel for viewing papers submitted by this user.
	private class ViewSubmittedPapersPanel extends
									AbstractPaperViewPanel {
		private static final long serialVersionUID =
				-8604369487284825034L;

		@Override
		protected JPanel createPaperInfoPanel(
				Manuscript paper) {
			JPanel panel = new JPanel();
			panel.setLayout(new BoxLayout(panel,
					BoxLayout.Y_AXIS));
			JLabel title = new JLabel("Title: " +
					paper.getPaperName());
			title.setAlignmentX(Component.CENTER_ALIGNMENT);
			JLabel date = new JLabel("Submitted On: " +
					paper.getSubmissionDate().toString());
			date.setAlignmentX(Component.CENTER_ALIGNMENT);
			panel.add(title);
			panel.add(date);
			return panel;
		}

		@Override
		protected void deletePaper(Manuscript paper) {
			myConf.delete(paper);
			paper.getAuthor().delete(paper);
			for (Reviewer r : paper.getReviewers()){
				if (r.getAssignedManuscripts().contains(paper)){
					r.delete(paper);
				}
				
			}
			ConferenceIO.saveConference(myPath, myConf);
			refreshPapers();
		}
		
		@Override
		public void refreshPapers() {
			removeAll();
			for(Manuscript paper : myConf.getManuscripts()) {
				if (paper.getAuthor().getName()
						.equals(myUser.getName())) {
					addPaper(paper);
				}
			}
			updateUI();
		}
	}
	
	private class ChooseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			if (chooser.showOpenDialog(
					SubmitPaperPanel.this)
					== JFileChooser.APPROVE_OPTION) {
				File chosen = chooser.getSelectedFile();
				if (chosen.getName().endsWith(".txt")) {
					viewtitle.setText(chooser
							.getSelectedFile().getPath());
					viewtitle.setVisible(true);
					//Default the title to filename
					paperName.setText(chosen.getName()
							.replaceFirst(".txt", ""));
					//enable name and submit
					paperName.setEnabled(true);
					submitBut.setEnabled(true);					
				} else {
					JOptionPane.showMessageDialog(
							SubmitPaperPanel.this,
							"Only .txt files allowed!",
							"Invalid paper type",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
	
	private class SubmitListener extends MouseAdapter {
		public void mouseClicked(MouseEvent e) {
			if (viewtitle.getText() != "") {
				Manuscript m = new Manuscript((Author)myUser,
						Calendar.getInstance().getTime(),
						paperName.getText(),
						new File(viewtitle.getText()));
				((Author) myUser).addManuscript(m);
				myConf.submitManuscript(m);
				if (ConferenceIO.saveConference(
						myPath, myConf)) {
					viewtitle.setText("");
					paperName.setText(NAME_PLACEHOLDER);
					submittedpanel.refreshPapers();
					submitBut.setEnabled(false);
					paperName.setEnabled(false);
				}
			}
		}
	}
}
